﻿using FrameworkTemplate.Workers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using User;

namespace FrameworkTemplate.Windows.Commands
{
    /// <summary>
    /// 打开窗口命令,防止多开窗口
    /// </summary>
    public class OpenAccountWindow : ICommand
    {
        public readonly static string name = "account_window";
        private AccountManager window;

        public event EventHandler CanExecuteChanged;

        public OpenAccountWindow()
        {
            GLOBAL.user.PropertyChanged += User_PropertyChanged;
        }

        private void User_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "right")
            {
                CanExecuteChanged?.Invoke(this, new EventArgs());
            }
        }

        public bool CanExecute(object parameter)
        {
            if (GLOBAL.user.right >= User.Right.Normal)
            {
                return true;
            }
            return false;
        }

        public void Execute(object parameter)
        {
            var windows = parameter as Dictionary<string, Window>;
            if (!windows.ContainsKey(name))
            {
                window = new AccountManager(ref GLOBAL.user);
                windows[name] = window;
                window.Closed += (object s, EventArgs args) =>
                {
                    if (windows.ContainsKey(name))
                        windows.Remove(name);
                };
                window.Show();
            }
        }
    }
}
